package com.ruifu.proj.controller;

import com.alibaba.fastjson.JSONObject;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.vo.ResultVO;
import com.ruifu.proj.dto.pmember.PmPMemberDTO;
import com.ruifu.proj.service.PmPMemberService;
import com.ruifu.proj.vo.pmember.PmPMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author nyr
 * @create 2019/8/14
 */
@RestController
@CrossOrigin
@RequestMapping("/pmPMember")
public class PmPMemberController {

    @Autowired
    private PmPMemberService pmPMemberService;

    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    /**
     * 批量添加项目团队成员
     *
     * @param pmPMemberDTOList
     * @return
     */
    @PostMapping("/insertPL")
    public ResultVO insertPL(@RequestBody List<PmPMemberDTO> pmPMemberDTOList) {
        try {
            pmPMemberService.insertPL(pmPMemberDTOList, cloudUser);
            return new ResultVO(true, "添加项目团队成员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "添加项目团队成员失败");
        }
    }

    /**
     * 根据id删除成员配置
     *
     * @param pbId 团队成员记录表id
     * @return
     */
    @GetMapping("/deleteById")
    public ResultVO deleteById(Long pbId) {
        try {
            pmPMemberService.deleteById(pbId, cloudUser);
            return new ResultVO(true, "删除项目团队成员成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "删除项目团队成员失败");
        }
    }

    /**
     * 根据项目/任务id查询项目团队成员列表
     *
     * @param pbPtId 当前项目id
     * @return
     */
    @GetMapping("/findByPbPtId/{pbPtId}")
    public ResultVO findByPbPtId(@PathVariable Long pbPtId) {
        try {
            List<PmPMemberVO> pmPMemberVOList = pmPMemberService.findByPbPtId(pbPtId, cloudUser);
            return new ResultVO(true, JSONObject.toJSONString(pmPMemberVOList));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, e.getMessage());
        }
    }
}
